package com.scada.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scada.model.bean.SysMenu;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.MenuAndPermissionVO;
import com.scada.model.bean.vo.MenuVO;
import com.scada.model.common.ZTree;
import com.scada.model.result.ResponseBean;
import com.scada.service.MenuService;


/**
 * @author haungsheng
 * 2016-10-13
 * 首页菜单menu方法
 *
 */
@Controller
@RequestMapping("menu")
public class MenuController extends BaseController{
	
	@Autowired
	private MenuService menuService;
	
	@RequestMapping(value = "MenuList")
	@ResponseBody
	public List<SysMenu>  MenuList(){
		//2016-12-12
		//方法修改 配置权限
		User user = getCurrentUser();
		List<SysMenu> menuList = null;
		//获取当前用户 如果是admin或root开放所有权限
//		if(StringUtils.equals("admin", user.getLoginName()) || StringUtils.equals("root", user.getLoginName())){
//			menuList= menuService.selectMenuIsleaf();
//		}else{
//			//根据用户查询菜单权限
//			menuList= menuService.findLeftMenuByUser(user.getId());
//		}
		menuList= menuService.selectMenuIsleaf();
		return menuList;
	}
	
	@RequestMapping(value = "TwoMeunList")
	@ResponseBody
	public List<MenuVO>  TwoMeunList(int id){
		//2016-12-12
		//方法修改 配置权限
		User user = getCurrentUser();
		List<MenuVO> menuList = null;
		//获取当前用户 如果是admin或root开放所有权限
//		if(StringUtils.equals("admin", user.getLoginName()) || StringUtils.equals("root", user.getLoginName())){
//			menuList= menuService.selectMenuList(id);
//		}else{
//			//根据用户查询菜单权限
//			menuList= menuService.findLeftMenuById(user.getId(), id);
//		}
		menuList= menuService.selectMenuList(id);
		return menuList;
	}
	
	/**
	 * 查询并拼接权限树数据
	 * @param ids
	 * @param model
	 * @return
	 */
	@RequestMapping("ztreePerData")
	@ResponseBody
	public String ztreePerData() {
		
		List<SysMenu> deptList =  menuService.selectAllData();
		
		List<ZTree> treeList = new ArrayList<ZTree>();
		
		deptList.forEach(perInfo -> {
			ZTree z = new ZTree();
			z.setId("permission" + perInfo.getId());
			z.setPid("permission" + perInfo.getPid());
			z.setName(perInfo.getSysMenuName());
			z.setGuid(String.valueOf(perInfo.getId()));
			z.setNocheck("false");
			treeList.add(z);
		});
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("nodes", treeList);
		map.put("success", true);
		return toJson(map);
	}
	
	@RequestMapping("selectAllMenuAndPermission")
	@ResponseBody
	public List<MenuAndPermissionVO> selectAllMenuAndPermission(){
		ResponseBean rb = menuService.selectAllMenuAndPermission();
		if(StringUtils.equals(ResponseBean.SUCCESS, rb.getType())){
			return (List<MenuAndPermissionVO>)rb.getObject("menuAndPerList");
		}
		return null;
	}
}
